var baseUrl = 'http://192.168.0.151:8083'; // 服务器地址
var pirUrl = 'http://61.144.241.236:10089/'; // 图片路径
var total = 0;
var pageSize = 20;
var pageNo = 1;
var parentCode = '';  // 当前城市编码
var countyObj = {};  // 当前城市所有县/区
var buscircleObj = {};  // 当前城市所有商圈
var cityName = '';  // 当前城市名
var categoryName = '';  // 一级类别 
var twoCategoryName = ''; // 二级类别 
var countyStr = '';  // 当前选择县/区
var buscircleStr = '';  // 当前选择商圈
var evaluation = 2 ;  // 2好评优先,3差评优先
var inteSort = 0;  // 0默认   1 店铺开通时间  2 服务发布时间

$(document).ready(function(){  
  parentCode = '410100';
  getCity();
  getCounty();
  getBusinessCircle(countyObj,true);
  getHotCategory();
  getSelect(0);
  getShopList();
});
// 一级类目 切换
$('#pid-box').on('click','li',function(){
  $(this).addClass('screenActive').siblings().removeClass('screenActive');
  if($(this).data('pid') == -1){
    categoryName = '';
    getSelect(0);
  }else{
    categoryName = $(this).text();
    getSelect($(this).data('pid'));
  };
  getShopList();
  console.log(categoryName);
});

// 县/区 切换
$('#county-box').on('click','li',function(){
  $(this).addClass('screenActive').siblings().removeClass('screenActive');
  if($(this).data('area-code') == -1){
    countyStr = '';
    getBusinessCircle(countyObj,true);
  }else{
    countyStr = $(this).text();
    getBusinessCircle($(this).data('area-code'),false);
  }
  getShopList();
  console.log(countyStr);
});

// 商圈切换
$('#buscircle-box').on('click','li',function(){
  $(this).addClass('screenActive').siblings().removeClass('screenActive');
  if($(this).data('area-id') == -1){
    buscircleStr = '';
  }else{
    buscircleStr = $(this).text();
  }
  getShopList();
});

// 二级分类选择
$('#sub-select').change(function(){
  if($(this).children('option:selected').val() == -1){
    twoCategoryName = '';
  }else{
    twoCategoryName = $(this).children('option:selected').text();
  };
  getShopList();
  console.log(twoCategoryName);
});

// 好评排序
$('.haoping').click(function(){
  if($('.haoping').children().hasClass('glyphicon-triangle-bottom')){
    $('.haoping').children().removeClass('glyphicon-triangle-bottom').addClass('glyphicon-triangle-top');
    evaluation = 3;
  }else{
    $('.haoping').children().removeClass('glyphicon-triangle-top').addClass('glyphicon-triangle-bottom');
    evaluation = 2;
  }
  getShopList();
  console.log(evaluation);
});

// 智能排序
$('#inte-sort').change(function(){
  inteSort = $(this).children('option:selected').val();
  getShopList();
  console.log(inteSort);
});

// 获取当前城市区/县
function getCounty(){
  countyObj = $.grep(gy_public_county, function(value) {
    //过滤筛选条件
    return value.parentCode == parentCode;
  });
  countyObj.unshift({areaName:'全部',areaCode:'-1'});
  var data = {
    data:countyObj
  };
  // console.log(data);
  var html = template('county-script', data);
  document.getElementById('county-box').innerHTML = html;
};

// 获取当前区/县商圈
function getBusinessCircle(data,isAll){
  var tempBuscircle = [];
  if(data == -1 || isAll){
    for(var i = 0 ; i < data.length ; i ++){
      buscircleObj = $.grep(gy_public_businessCircle, function(value) {
      //过滤筛选条件
        if(value.areaCode == data[i].areaCode){
          tempBuscircle.push(value)
        }
      });
    };
  }else{
    buscircleObj = $.grep(gy_public_businessCircle, function(value) {
      //过滤筛选条件
      if(value.areaCode == data){
        tempBuscircle.push(value)
      }
    });
  }
  buscircleObj = tempBuscircle;
  // console.log(buscircleObj);
  buscircleObj.unshift({locationName:'全部',id:'-1'});
  var data = {
    data:buscircleObj
  };
  // console.log(data);
  var html = template('buscircle-script', data);
  document.getElementById('buscircle-box').innerHTML = html;
};

// 获取当前城市
function getCity(){
  var cityObj = $.grep(gy_public_city, function(value) {
    //过滤筛选条件
    return value.areaCode == parentCode; 
  });
  cityName = cityObj[0].areaName;
};

// 获取热门类目
function getHotCategory(){
  $.ajax({
    url: baseUrl+"/hsent-swfw-user/homeCategoryController/findHomeCategory",
    type:"get",	
    data:"",	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
        var data={
          data:res.data,
        }
        var html = template('top-nav-script', data);
        document.getElementById('top-nav').innerHTML = html;
      }
    },
    error:function(res){

    },
  })
};

// 获取分类选择  一级传 0  二级传 父 id
function getSelect(data){
  var pid = data;
  $.ajax({
    url: baseUrl+"/hsent-swfw-user/homePageController/findCategoryByPid",
    type:"get",	
    data:"cid="+pid,	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
        var pidArr = res.data;
        if(pid == 0){
          pidArr.unshift({categoryName:'全部',id:'-1'});
          console.log(pidArr)
          var data = {
            data:pidArr
          };
          var html = template('pid-script', data);
          document.getElementById('pid-box').innerHTML = html;
          var data2 = {
            data:[{categoryName:'全部分类',id:'-1'}]
          };
          var html2 = template('sub-select-script', data2);
          // console.log(document.getElementById('sub-select'))
          document.getElementById('sub-select').innerHTML = html2;
        }else{
          console.log(1)
          pidArr.unshift({categoryName:'全部分类',id:'-1'});
          console.log(pidArr)
          var data = {
            data:pidArr
          };
          var html = template('sub-select-script', data);
          document.getElementById('sub-select').innerHTML = html;
        }
      }
    },
    error:function(res){

    },
  })
};

// 获取店铺信息
function getShopList(){
  $.ajax({
    url: baseUrl+"/hsent-swfw-user/homePageController/findPageByCondition",
    type:"get",	
    data:"pageNo="+pageNo+
    "&pageSize="+pageSize+
    "&categoryName="+twoCategoryName+
    "&orderstatus="+evaluation+
    // "&orderstatus="+''+
    "&areaName="+countyStr+
    "&cityName="+cityName+
    "&locationName="+buscircleStr+
    "&inteSort="+inteSort,	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
        total = res.total;
        var tempData = res.data;
        for(var i = 0 ; i < tempData.length ; i ++){
          tempData[i].categoryNames = tempData[i].categoryNames.split(',');
          tempData[i].round = Math.round(tempData[i].shopEvaluateValue);
          tempData[i].cha = 5 - tempData[i].round;
          tempData[i].entResno = tempData[i].entResno.substring(0,2)+' '+
          tempData[i].entResno.substring(2,5)+' '+tempData[i].entResno.substring(5,7)+' '+
          tempData[i].entResno.substring(7,11)
        }
        var data={
          data:tempData,
          pirUrl:pirUrl
        }
        var html = template('content-script', data);
        document.getElementById('content-box').innerHTML = html;
        layuiPage();
      }
    },
    error:function(res){

    },
  })
}

// 分页器
function layuiPage(){
	layui.use('laypage', function(){
		var laypage = layui.laypage;
		//执行一个laypage实例
		laypage.render({
			elem: 'Pagination', //注意，这里的 test1 是 ID，不用加 # 号
			count: total, //数据总数，从服务端得到
			limits:[20,30,40,100],
			limit:pageSize,
			curr:pageNo,
			layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
			jump: function(obj,first){
				console.log('我是分页')
				pageNo = obj.curr;
				pageSize = obj.limit;
				if(!first){ //首次不执行
					getShopList();
				}  
			}
		});
	});
}